/**
 *    Copyright (C) 2009 Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_DYNVECTOR_FACTORY_HPP
#define	_CUBEFS_CORE_DYNVECTOR_FACTORY_HPP

#include <cubefs/core/container/tools.hpp>
#include <cubefs/core/container/container.hpp>

#include <cubefs/core/btree/btree.hpp>

#include <cubefs/core/dynvector/names.hpp>
#include <cubefs/core/dynvector/pages/data_page.hpp>

#include <cubefs/core/dynvector/model/insert.hpp>
#include <cubefs/core/dynvector/model/remove.hpp>
#include <cubefs/core/dynvector/model/tools.hpp>

#include <cubefs/core/tools/types/typelist.hpp>
#include <cubefs/core/tools/assert.hpp>

#include <cubefs/api/models/model.hpp>

namespace cubefs    {
namespace ctr       {

using namespace cubefs::btree;
using namespace cubefs::dynvector;
using namespace cubefs::tools;


template <
    typename DataPageType_,
    typename BufferType_,
    typename BufferContentDescriptorType_,
    typename CountDataType_,
    typename Base
>
struct DynVectorModelTypes: public Base {

    typedef typename AppendTool<
                    typename Base::DataPageTypesList,
                    typename TypeListTool<
                        DataPageType_
                    >::List
    >::Result                                                                   DataPageTypesList;

    typedef DataPageType_                                                       DataPageType;
    typedef BufferType_                                                         BufferType;
    typedef BufferContentDescriptorType_                                        BufferContentDescriptorType;
    typedef CountDataType_                                                      CountDataType;
};


template <typename Profile, typename ModelTypeName_>
struct DynVectorTypes: public BTreeTypes<Profile, ModelTypeName_, IDType> {
    typedef BTreeTypes<Profile, ModelTypeName_, IDType> Base;

    typedef NullType                                                            DataPagePartsList;

    static const bool MapType                                                   = MapTypes::Index;

    typedef typename AppendTool<
                    typename Base::ModelPartsList,
                    typename TypeListTool<
                        cubefs::dynvector::ToolsName,
                        cubefs::dynvector::RemoveName,
                        cubefs::dynvector::InsertName
                    >::List
    >::Result                                                                   ModelPartsList;


    typedef cubefs::api::Data                                                   BufferType;
    typedef NullType                                                            BufferContentDescriptorType;
    typedef NullType                                                            CountDataType;


    template <Int Size>
    struct DataBlockTypeFactory {
        typedef NullType                                                        Type;
    };
};








template <
        typename Profile,
        typename ModelTypes,
        typename DefaultType
>
class TypeFactory<
        RootProfile,
        Profile,
        cubefs::dynvector::DynVector<
            ModelTypes
        >,
        DefaultType
>: public TypeFactory<RootProfile, Profile, BTree<ModelTypes>, DefaultType> {

    typedef TypeFactory<RootProfile, Profile, BTree<ModelTypes>, DefaultType>   Base;

public:

    typedef typename ModelTypes::DataPagePartsList                              DataPagePartsList;

    CUBEFS_STATIC_ASSERT(IsList<DataPagePartsList>::Value);

    typedef typename ContainerTypesLocator<Profile>::PageManagerType			PageMgr;

    typedef DataPage<
                Profile,
                DataPagePartsList,
                ModelTypes::template DataBlockTypeFactory,
                cubefs::btree::TreePage<PageMgr>
    >                                                                           DataPageType;

    typedef DynVectorModelTypes<
                DataPageType,
                typename ModelTypes::BufferType,
                typename ModelTypes::BufferContentDescriptorType,
                typename ModelTypes::CountDataType,
                typename Base::Types
    >                                                                           Types;



    typedef Model<
                typename Base::ModelTypeName,
                typename Base::ModelPartsList,
                Types
    >                                                                           Type;
};

}
}
#endif
